﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Principal;
using System.Web;
using System.Web.UI;
using FlexySys.Common;
using System.Data.Linq;
using System.ComponentModel;

namespace FlexySys.Web.Core
{

    #region BaseUser

    public class BaseUser : IPrincipal
    {

        public Guid? Id { get; private set; }
        public string Culture { get; set; }
        public TimeZoneInfo TimeZone { get; private set; }
        public string Theme { get; set; }

        public BaseUser()
        {
            Init(HttpContext.Current.User.Identity.Name);
        }

        private void Init(string userName)
        {
            var db = new DataLayer.FlexySysEntities();


            var user = db.aspnet_Users.First(x => x.UserName == userName);
            if (user != null)
            {
                this.Id = user.UserId;
                this.Theme = user.Theme;
                this.Culture = user.Culture;
                if (!string.IsNullOrEmpty(user.TimeZone))
                    this.TimeZone = TimeZoneInfo.FindSystemTimeZoneById(user.TimeZone);

                Sessions<BaseUser>.Set(this);
            }

        }

        #region IPrincipal Members

        public IIdentity Identity
        {
            get { return HttpContext.Current.User.Identity; }
        }

        public bool IsInRole(string role)
        {
            return HttpContext.Current.User.IsInRole(role);
        }

        #endregion

    }

    #endregion


    #region Base

    public class BasePage : Page
    {

        public BasePage()
        {

        }

        protected void Refresh()
        {
            Response.Redirect(Request.RawUrl);
        }

        protected override void OnPreInit(EventArgs e)
        {
            if (this.User != null && this.User.Identity.IsAuthenticated)
            {
                if (!string.IsNullOrEmpty(this.User.Theme))
                    this.Page.Theme = this.User.Theme;

                if (!string.IsNullOrEmpty(this.User.Culture))
                {
                    this.Page.Culture = this.User.Culture;
                    this.Page.UICulture = this.User.Culture;
                }

            }
            base.OnPreInit(e);
        }
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
        }

        public new BaseUser User
        {
            get
            {
                var user = Sessions<BaseUser>.Get();
                if (user == null && this.Page.User.Identity.IsAuthenticated)
                {
                    user = new BaseUser();
                    Sessions<BaseUser>.Set(user);
                }

                return user;
            }
        }

    }

    public class DocumentPage : BasePage
    {
        public delegate void OnCommandHandler(object sender, CancelEventArgs e);
        public delegate void OnCommandComplateHandler(object sender);

        public event EventHandler<CancelEventArgs> OnCommand;
        public event EventHandler<EventArgs> OnCommandComplate;

        public string DocumentType { get; set; }

        //public virtual void OnBeginSaving(CancelEventArgs e)
        //{
        //    EventHandler<CancelEventArgs> handler = BeginSaving;
        //    if (handler != null)
        //    {
        //        handler(this, e);
        //    }
        //}
    }



    public class BaseControl : UserControl
    {
        public BaseControl()
        {

        }

        public new BasePage Page { get { return base.Page is BasePage ? (BasePage)base.Page : (BasePage)null; } }
    }

    public class CommonControl : UserControl
    {
        public CommonControl()
        {

        }

        public new DocumentPage Page { get { return base.Page is DocumentPage ? (DocumentPage)base.Page : (DocumentPage)null; } }
    }

    #endregion

    
}